package com.fyy.rtspdemo.result;

import com.alibaba.fastjson2.JSONObject;

import java.util.HashMap;

public class JsonResult extends HashMap<String,Object> {

    public static final int SUCCESS = 200;

    public JsonResult(){}

    public static JsonResult ok(){
        return ok("操作成功");
    }

    public static JsonResult okFallBack() {
        return okFallBack("操作成功");
    }

    public JsonResult put(Object obj) {
        return this.put("data", obj);
    }

    public static JsonResult error() {
        return error("操作失败");
    }

    public static JsonResult error(String message) {
        return error(500, message);
    }

    public static JsonResult error(int code, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.put("timestamp", System.currentTimeMillis());
        jsonResult.put("status", code);
        jsonResult.put("message", message);
        return jsonResult;
    }

    public JsonResult setCode(int code) {
        super.put("status", code);
        return this;
    }

    public JsonResult setMessage(String message) {
        super.put("message", message);
        return this;
    }

    public static JsonResult okFallBack(String message) {
        return result(205, message);
    }


    public static JsonResult ok(String msg){
        return result(200,msg);
    }

    public static JsonResult result(int code, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.put("timestamp", System.currentTimeMillis());
        jsonResult.put("status", code);
        jsonResult.put("message", message);
        return jsonResult;
    }

    @Override
    public JsonResult put(String key, Object object) {
        super.put(key, object);
        return this;
    }

    public static JsonResult forbidden(String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.put("timestamp", System.currentTimeMillis());
        jsonResult.put("status", 401);
        jsonResult.put("message", message);
        return jsonResult;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    public JSONObject toJSONObject() {
        return JSONObject.parseObject(toString());
    }


}
